<template>
  <div>
    <a-modal
        title="修改管理员信息"
        :visible="visible"
        @cancel="cancel"
        @ok="ok"
    >
      <a-form-model :model="adminInfo" :rules="rules" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }" ref="form">
        <a-form-model-item label="姓名" prop="name">
          <a-input
              v-model="adminInfo.name"
          />
        </a-form-model-item>
        <a-form-model-item label="账号" prop="account">
          <a-input
              v-model="adminInfo.account"
          />
        </a-form-model-item>
        <a-form-model-item label="密码" prop="password">
          <a-input
              v-model="adminInfo.password"
          />
        </a-form-model-item>

        <a-form-model-item label="所属学院" prop="college">
          <a-select
              v-model="adminInfo.college"
              style="width: 100%;"
          >
            <a-select-option v-for="item in colleges" :key="item.collegeId" :value="item.collegeId" >{{item.name}}</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </div>
</template>

<script>


import {mapState} from "vuex";
const rules={
      name:[{ required: true, message: '姓名不能为空', trigger: 'blur' }],
      account:[{ required: true, message: '账号不能为空', trigger: 'blur' }],
      password:[{ required: true, message: '密码不能为空', trigger: 'blur' }],
      college:[{ required: true, message: '学院不能为空', trigger: 'blur' }],
    }
export default {
  name: "AddAndModifyCollegeAdmin",
  data(){
    return{
      rules,
      collegeAdminId:'',
      visible:false,
      flag:false,
      adminInfo:{
        name:"",
        account:"",
        password:"",
        college:"",
      },
      colleges:[{name:"计算机科学与工程学院",collegeId:"123546"}]
    }
  },
  methods:{
    ok(){
      if (this.flag==true){
        this.$refs.form.validate(value=>{
          if(value){//检验为真时，触发
            const collegeAdminRecord={
              name:this.adminInfo.name,
              account:this.adminInfo.account,
              collegeId:this.adminInfo.college,
              collegeAdminId:this.collegeAdminId,
              password:this.adminInfo.password,
              college:"null" //不要学院
            }
            this.$store.dispatch('collegeAdmin/changeAdminInfo',{collegeAdminRecord:collegeAdminRecord,token:this.accessState.token})
                .then(()=>{
                  this.$message.success("修改成功");
                  this.visible = false;
                  this.$refs.form.resetFields();//重置表单数据（重置到所附的值）
                  this.$emit("over")
                }).catch(err=>{
              this.$message.error(err.msg);
            });
          }else {
            this.$message.error("请正确填写表单");
          }
        })
      }else{
        this.$refs.form.validate(value=>{
          if(value){//检验为真时，触发
            const collegeAdminRecord={
              name:this.adminInfo.name,
              account:this.adminInfo.account,
              collegeId:this.adminInfo.college,
              collegeAdminId:this.collegeAdminId,
              password:this.adminInfo.password,
              college:"null" //不要学院
            }
            this.$store.dispatch('collegeAdmin/addNewAdmin',{collegeAdminRecord:collegeAdminRecord,token:this.accessState.token})
                .then(()=>{
                  this.$message.success("添加成功");
                  this.visible = false;
                  this.$refs.form.resetFields();//重置表单数据（重置到所附的值）
                  this.$emit("over")
                }).catch(err=>{
              this.$message.error(err);
            });
          }else {
            this.$message.error("请正确填写表单");
          }
        })
      }
    },

    showWindow(record){
      this.collegeAdminId=record.colAdmId;
      this.$store.dispatch("college/getCollegeList",{token:this.accessState.token}).then(
          ()=>{
            this.colleges=this.collegeState.collegeNameId;
          }
      ).catch(err=>{
        this.$message.info(err.msg);
      })// 获取学院列表信息
      if(record.colAdmId==null){
        this.adminInfo.name="";
        this.adminInfo.password="";
        this.adminInfo.account="";
        this.adminInfo.college=record.collegeId
        this.flag=false;
      }else {
          this.$store.dispatch("college/getAdminInfo",{token:this.accessState.token,colAdmId:record.colAdmId}).then((data)=>{
          this.adminInfo.name=data.data.name;
          this.adminInfo.account=data.data.account;
          this.adminInfo.password=data.data.password;
          this.adminInfo.college=data.data.collegeId;
          this.flag=true;
        }).catch((err)=>{
          this.$message.error(err.msg)
        });
      }
      this.visible=true;

    },
    cancel(){
      this.adminInfo.name="";
      this.adminInfo.password="";
      this.adminInfo.account="";
      this.adminInfo.college="";
      this.$refs.form.resetFields();//重置表单数据（重置到所附的值）
      this.flag=false;
      this.visible=false
    }
  },
  computed: {
    ...mapState({ // 这个是提取store中access文件夹目录下的数据
      'accessState': 'access',
      'collegeState':'college'
    })
  }
}
</script>

<style scoped>

</style>